<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<html>

<head>
    <meta charset="UTF-8">
    <title>知识库-后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet"  th:href="@{/css/font.css}">
    <link rel="stylesheet"  th:href="@{/css/xadmin.css}">
    <script type="text/javascript" th:src="@{/plugins/jquery/jquery.min.js}" ></script>
    <script  th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <input name="aid" type="hidden" lay-verify="aid" th:value="${article.aid}">
        <input name="id" type="hidden" lay-verify="id" th:value="${article.id}">
        <div class="layui-form-item">
            <label id="title" class="layui-form-label">
                标题
            </label>
            <div class="layui-input-inline">
                <input type="text" id="A_title" name="title" required="" lay-verify="title"
                       autocomplete="off" class="layui-input" th:value="${article.title}">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <select name="cid" id="classity" lay-verify="cid">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label id="A_edit" class="layui-form-label">
                编辑内容
            </label>
            <div class="layui-input-inline">
                <textarea id="demo" name="scontent" autocomplete="off" lay-verify="scontent" th:text="${article.scontent}" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="A_edit" class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="edit" lay-submit="" style="background-color: #6496dc">
                修改
            </button>
            <button  class="layui-btn" lay-filter="return" lay-submit="" style="background-color: #6496dc">
                返回
            </button>
        </div>
    </form>
</div>
<script>
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/user/uploadHeadPic' //接口url
                ,type: '' //默认post
            }
        });
//注意：layedit.set 一定要放在 build 前面，否则配置全局接口将无效。
        layedit.build('demo'); //建立编辑器
    });

    layui.use('upload', function(obj){
        var upload = layui.upload
            ,layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/user/uploadHeadPic' //接口url
                ,type: '' //默认post
            }
        });
//注意：layedit.set 一定要放在 build 前面，否则配置全局接口将无效。
        var index = layedit.build('demo'); //建立编辑器
        layui.use(['form', 'upload', 'layer'],function(){
            var form = layui.form;
            $.ajax({
                url: '/article/classify_list',
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    console.log(data);//下面会提到这个data是什么值
                    //使用循环遍历，给下拉列表赋值
                    $.each(data.data, function (index, value) {
                        // console.log(value.department_id);
                        $('#classity').append(new Option(value.cla,value.cid));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                }
            })
        });

        //监听提交
        form.on('submit(edit)', function(data){
            //前端将对象转json字符串
            console.log(JSON.stringify(data.field));
            data.field.scontent = layedit.getContent(index);
            //Ajax提交数据
            $.ajax({
                url:"/article/edit",
                dataType:"json",
                data:data.field,
                success:function (data) {
                    if (data.code==0){
                        layer.alert("修改成功", {icon: 6},function () {
                            parent.location.reload();//修改成功后刷新父界面
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                    else{
                        layer.alert("修改失败");
                    }

                }
            });

            return false;
        });
    });
</script>
</body>
</html>